﻿#define  _CRT_SECURE_NO_WARNINGS 1
#include "snake.h"
void SetPos(int x, int y)
{
	COORD pos = { x,y };
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(handle, pos);
}

void CreateFood(pSnake ps)
{
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreateFood():malloc():");
		return;
	}

	int x = 0;
	int y = 0;

	do
	{
		x = rand() % 53 + 2;
		y = rand() % 24 + 1;

	} while (x % 2 != 0);
	pSnakeNode cur = ps->pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			do
			{
				x = rand() % 53 + 2;
				y = rand() % 25 + 1;

			} while (x % 2 != 0);
		}
		cur = cur->next;
	}
	pFood->x = x;
	pFood->y = y;
	SetPos(pFood->x, pFood->y);
	wprintf(L"%lc", FOOD);
	ps->Food = pFood;

}

void InitSnake(pSnake ps)
{
	//蛇身有5个节点
	pSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake():malloc:");
			return;
		}
		//初始化蛇的位置
		cur->x = 24 + 2 * i;
		cur->y = 5;
		cur->next = NULL;
		if (ps->pSnake == NULL)
			ps->pSnake = cur;
		else
		{
			//头插法
			cur->next = ps->pSnake;
			ps->pSnake = cur;
		}
	}
	//打印蛇
	cur = ps->pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//其他的初始化
	ps->dir = RIGHT;
	ps->Food = NULL;
	ps->FoodWeight = 10;
	ps->score = 0;
	ps->sleeptime = 200;
	ps->status = OK;

	//食物的创建
	CreateFood(ps);

}

void CreateMap()
{
	int i = 0;
	SetPos(0, 0);
	for (int i = 0; i < 56; i+=2)
	{
		wprintf(L"%lc", WALL);
	}
	SetPos(0, 25);
	for (int i = 0; i < 56; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	for (int i = 0; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	for (int i = 0; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
	SetPos(65, 15);
	printf("用↑.↓.←.→ 分别控制蛇的移动\n");
	SetPos(65, 16);
	printf("F3为加速，F4为减速");


}

void WelcomeToGame()
{
	system("cls");
	SetPos(38, 12);
	printf("欢迎来到贪吃蛇游戏\n");
	SetPos(40, 25);
	system("pause");
	system("cls");
	SetPos(27, 12);
	printf("用↑.↓.←.→ 分别控制蛇的移动， F3为加速，F4为减速\n");
	SetPos(40, 25);
	system("pause");
	system("cls");
}

void GameStart(pSnake ps)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO Info;
	GetConsoleCursorInfo(handle, &Info);
	Info.bVisible = false;
	SetConsoleCursorInfo(handle, &Info);
	
	//游戏的欢迎界面
	WelcomeToGame();
	//游戏地图
	CreateMap();
	//蛇的初始化
	InitSnake(ps);
}


void PrintHelp()
{
	SetPos(65, 14);
	printf("不能撞墙，不能咬到自己");
	SetPos(65, 17);
	printf("ESC：退出游戏，SPACE：暂停游戏");


}

void pause()
{
	while (1)
	{
		Sleep(100);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

void EatFood(pSnakeNode pNext,pSnake ps)
{
	//头插
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;
	pSnakeNode cur = ps->pSnake;

	//打印蛇
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->score += ps->FoodWeight;
	free(ps->Food);
	ps->Food = NULL;
	CreateFood(ps);
}

void NotEatFood(pSnakeNode pNext, pSnake ps)
{
	//头插
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;
	pSnakeNode cur = ps->pSnake;
	//打印蛇
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;

}

void Kill_By_Wall(pSnake ps)
{
		if (ps->pSnake->x == 0 ||
			ps->pSnake->x == 56 ||
			ps->pSnake->y == 0 ||
			ps->pSnake->y == 25)
		{
			ps->status = KILL_BY_WALL;
		}
}

void Kill_By_Self(pSnake ps)
{
	//从第二个节点开始
	pSnakeNode cur = ps->pSnake->next;
	while (cur)
	{
		if (cur->x == ps->pSnake->x && cur->y == ps->pSnake->y)
		{
			ps->status = KILL_BY_SELF;
			return ;
		}
		cur = cur->next;
	}
}

void SnakeMove(pSnake ps)
{
	//创建下一个节点来让蛇进行移动
	pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNext == NULL)
	{
		perror("SnakeMove():malloc:");
		return;
	}
	pNext->next = NULL;
	//根据坐标和方向来确定下一个节点的蛇头方向和坐标
	switch (ps->dir)
	{
	case UP:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y-1;
		break;
	case DOWN:
		pNext ->x= ps->pSnake->x;
		pNext ->y= ps->pSnake->y + 1;
		break;	
	case LEFT:
		pNext->x = ps->pSnake->x-2;
		pNext->y = ps->pSnake->y;
		break;	
	case RIGHT:
		pNext->x = ps->pSnake->x+2;
		pNext->y = ps->pSnake->y;
		break;
	}

	//判断下一个节点是否是食物
	if (ps->Food->x == pNext->x && ps->Food->y == pNext->y)
	{
		EatFood(pNext,ps);
	}
	else
	{
		NotEatFood(pNext, ps);
	}

	//撞墙会死
	Kill_By_Wall(ps);
	//撞到自己会死
	Kill_By_Self(ps);
}

void GameRun(pSnake ps)
{
	//打印帮助信息
	PrintHelp();
	//打印已获得的分数和每个食物的分数
	do
	{
		SetPos(65, 11);
		printf("总分：%d", ps->score);
		SetPos(65, 12);
		printf("每个食物的分数:%2d", ps->FoodWeight);

		if (KEY_PRESS(VK_UP) && ps->dir != DOWN)
		{
			ps->dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->dir != UP)
		{
			ps->dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT)
		{
			ps->dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT)
		{
			ps->dir = RIGHT;
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = ESC;
			break;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->sleeptime >= 80)
			{
				ps->sleeptime -= 30;
				ps->FoodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->FoodWeight > 2)
			{
				ps->sleeptime += 30;
				ps->FoodWeight -= 2;
			}
		}
		//蛇休眠的时间
		Sleep(ps->sleeptime);
		//是否吃到食物的移动
		SnakeMove(ps);

	} while (ps->status == OK);
}


void GameEnd(pSnake ps)
{
	pSnakeNode cur = ps->pSnake;
	//游戏结束的方式
	switch (ps->status)
	{
	case ESC:
		SetPos(16, 12);
		printf("游戏结束，正常退出\n");
		break;
	case KILL_BY_WALL:
		SetPos(16, 12);
		printf("很遗憾，撞墙了，游戏结束\n");
		break;
	case KILL_BY_SELF:
		SetPos(16, 12);
		printf("很遗憾，撞到自己了，游戏结束\n");
		break;
	}
	//释放蛇
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}